package eg.backend.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import eg.backend.base.repository.EntityRepositoryImpl;
import eg.backend.domain.GlobalTag;
import eg.backend.domain.Tag;
import eg.backend.domain.fly.TagCount;
import eg.backend.repository.TagRepository;
import eg.backend.util.StringUtil;

public class TagRepositoryImpl extends EntityRepositoryImpl<Tag, String> implements TagRepository {
	@Override
	public Tag get(String identifier) {
		return this.baseDao.get(identifier, Tag.class);
	}
	
	public List<TagCount> findUsedTags(String owner) {
		String queryName = "Tag.findUsedTags";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("owner", owner);
		return baseDao.findByNamedQuery(queryName, Integer.MAX_VALUE, queryParams);
	}

	public GlobalTag getGlobalTag(String name) {
		String queryName = "Tag.getGlobalTag";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("name", name);
		return baseDao.getByNamedQuery(queryName, queryParams);
	}

	public List<String> findByNameAndOwner(String name, String owner) {
		String queryName = "Tag.findByOwner";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("owner", owner);
		if(!StringUtil.isEmpty(name)) {
			queryName = "Tag.findByNameAndOwner";
			queryParams.put("name", name);
		}
		return baseDao.findByNamedQuery(queryName, Integer.MAX_VALUE, queryParams);
	}

	@Deprecated
	public List<String> findNotUsedByNameAndOwner(String name, String owner) {
		String queryName = "Tag.findNotUsedByOwner";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("owner", owner);
		if(!StringUtil.isEmpty(name)) {
			queryName = "Tag.findNotUsedByNameAndOwner";
			queryParams.put("name", name);
		}
		return baseDao.findByNamedQuery(queryName, Integer.MAX_VALUE, queryParams);
	}

	public Tag getByNameAndOwner(String name, String owner) {
		String queryName = "Tag.getByNameAndOwner";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("owner", owner);
		queryParams.put("name", name);
		return this.getByNamedQuery(queryName, queryParams);
	}

	public List<String> findAllGlobalTagNames() {
		String queryName = "Tag.findAllGlobalTagNames";
		Map<String, Object> queryParams = new HashMap<String, Object>();
		return baseDao.findByNamedQuery(queryName, Integer.MAX_VALUE, queryParams);
	}

}
